//
//  FileHelper.m
//  restaurant
//
//  Created by dongpoyezi on 5/3/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FileHelper.h"


@implementation FileHelper

+(NSString *)getDocumentDirector{
	//NSDocumentDirectory means we look for Document directory under our application folder
	//NSUserDomainMask  means we restricted search to our application's sandbox
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentDirector =[paths objectAtIndex:0];
	return documentDirector;
}

+(NSString*)regionFilePath{
	return [[self getDocumentDirector] stringByAppendingPathComponent: regionFileName];
}

+(NSString*)dataFilePath{
	return [[self getDocumentDirector] stringByAppendingPathComponent: kFilename];
}

+(void) checkAndCreateDatabase{
	// Check if the SQL database has already been saved to the users phone, if not then copy it over
	BOOL success;
	
	// Create a FileManager object, we will use this to check the status
	// of the database and to copy it over if required
	NSFileManager *fileManager = [NSFileManager defaultManager];
	
	// Check if the database has already been created in the users filesystem
	success = [fileManager fileExistsAtPath:[self dataFilePath]];
	
	// If the database already exists then return without doing anything
	if(success) return;
	
	// If not then proceed to copy the database from the application to the users filesystem
	
	// Get the path to the database in the application package
	NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kFilename];
	
	// Copy the database from the package to the users filesystem
	[fileManager copyItemAtPath:databasePathFromApp toPath:[self dataFilePath] error:nil];
	
	[fileManager release];
}


+(NSMutableDictionary*)readDataFromFile:(NSString *) filePath{
	NSLog(@"file path ---%@",filePath);
	NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]init];
	if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
		NSLog(@"retrive data from ---%@",filePath);
		//[dataDic release];
		//dataDic = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
		dataDic = [dataDic initWithContentsOfFile:filePath];
	}
	
	return dataDic;
}

+(NSMutableDictionary*)readDataFromRegion{
	NSString *filePath =[self regionFilePath];
	return	[self readDataFromFile:filePath];
}

@end
